<?php 
defined('IN_IXDCW') or exit('Access Denied');

class Webpage extends BasicModule {

	private $_itemid;
	private $_moduleid;
	private $_item;
	private $_table;
	private $_fields;

    function __construct() {
	    parent::__construct();
		global $db,$moduleid;
	    $this->_moduleid  = $moduleid;
		$this->_table = $db->getPre().'webpage';
		$this->_fields = array('item','areaid', 'title','level','style','content','seo_title','seo_keywords','seo_description','editor','edittime','template', 'islink', 'linkurl','domain');
    }

	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function setItem($item) {
		$this->_item = $item;
	}

	public function getItem() {
		return $this->_item;
	}

	public function getFields() {
		return $this->_fields;
	}

	function pass($post) {
		global $L;
		if(!is_array($post)) return false;
		if(!$post['title']) return $this->err($L['webpage_pass_title']);
		if(isset($post['islink'])) {
			if(!$post['linkurl']) return $this->err($L['webpage_pass_linkurl']);
		} else {
			if($post['filepath'] && !preg_match("/^[0-9a-z_\-\/]+\/$/", $post['filepath'])) return $this->err($L['webpage_pass_path']);
			if($post['filename'] && !preg_match("/^[0-9a-z_\-]+\.[a-z]+$/", $post['filename'])) return $this->err($L['webpage_pass_name']);
			if($post['filename']) {
				if($this->_itemid) {
					$r = $this->getOne();
					if($r['linkurl'] != $post['filepath'].$post['filename']) {
						if(is_file(DT_ROOT.'/'.$post['filepath'].$post['filename'])) return $this->err($L['webpage_pass_exist']);
					}
				} else {
					if(is_file(DT_ROOT.'/'.$post['filepath'].$post['filename'])) return $this->err($L['webpage_pass_exist']);
				}
			}
		}
		return true;
	}

	function set($post) {
		global $MOD, $DT_TIME, $_username, $_userid;
		$post['islink'] = isset($post['islink']) ? 1 : 0;
		$post['edittime'] = $DT_TIME;
		$post['editor'] = $_username;
		if($post['content'] && isset($post['clear_link'])) $post['content'] = Post::clearLink($post['content']);
		if($post['content'] && isset($post['save_remotepic'])) $post['content'] = addslashes(Post::saveRemote($post['content'], $MOD['moduledir']));
		clear_upload($post['content']);
		if ($this->_itemid) {
			// 修改信息
			$r   = $this->getOne();
			$post['editor'] = $_username;
			$new            = $post['content'];
			$old = $r['content'];
			Post::deleteDiff($new, $old);
		}else {
		}
		return $post;
	}

	function getOne() {
        return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid=$this->_itemid");
	}

	function getList($condition = 'status=3', $order = 'listorder DESC, itemid DESC') {
		global $MOD, $pages, $page, $pagesize, $offset;
		$r = $this->_db->getOne("SELECT COUNT(*) AS num FROM {$this->_table} WHERE $condition");
		$pages = pages($r['num'], $page, $pagesize);		
		$lists = array();
		$result = $this->_db->query("SELECT * FROM {$this->_table} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while($r = $this->_db->fetchArray($result)) {
			$r['title'] = set_style($r['title'], $r['style']);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$r['linkurl'] = $r['domain'] ? $r['domain'] : linkurl($r['linkurl'], 1);
			$lists[] = $r;
		}
		return $lists;
	}

	function add($post) {
		global $DT, $module;
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$this->_itemid = $this->_db->insertId();
		if(!$post['islink']) {
			if($post['filename']) {
				$linkurl = $post['filepath'].$post['filename'];
			} else {
				$linkurl = $post['filepath'].$this->_itemid.'.'.$DT['file_ext'];
			}
			$this->_db->query(Sql::update($this->_table,"linkurl='$linkurl',listorder=$this->_itemid","itemid=$this->_itemid"));
		}
		return $this->_itemid;
	}

	function edit($post) {
		global $DT, $module;
		$this->delete($this->_itemid, false);
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table, $post, "itemid={$this->_itemid}", '', $this->_fields));
		if(!$post['islink']) {
			if($post['filename']) {
				$linkurl = $post['filepath'].$post['filename'];
			} else {
				$linkurl = $post['filepath'].$this->_itemid.'.'.$DT['file_ext'];
			}
			$this->_db->query(Sql::update($this->_table,"linkurl='$linkurl'","itemid=$this->_itemid"));
		}
		return true;
	}

	function delete($itemid, $all = true) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { 
				$this->delete($v, $all); 
			}
		} else {
			$this->_itemid = $itemid;
			$r = $this->getOne();
			if(!$r['islink']) {
				$_file = DT_ROOT.'/'.$r['linkurl'];
				if(is_file($_file)) unlink($_file);
			}
			if($all) {
				$userid = get_user($r['editor']);
				if($r['content']) Post::deleteDiff('', $r['content']);;
				$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
			}
		}
	}

	function order($listorder) {
		if(!is_array($listorder)) return false;
		foreach($listorder as $k=>$v) {
			$k = intval($k);
			$v = intval($v);
			$this->_db->query(Sql::update($this->_table,"listorder=$v","itemid=$k"));
		}
		return true;
	}


	function level($itemid, $level) {
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$this->_db->query(Sql::update($this->_table,"level=$level","itemid IN ($itemids)"));
	}

}
?>